/*

 Copyright (c) 2008-2009 Fabrizio Pistonesi

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 */
#include "cursor.h"

Cursor::Cursor(IrrlichtDevice *device)
  {
    //ctor
    Device = device;
    position2d<s32> position = position2d<s32> (
            Device->getVideoDriver()->getScreenSize().Width / 2 - Hot.X,
            Device->getVideoDriver()->getScreenSize().Height / 2 - Hot.Y);
    Icon = Device->getVideoDriver()->getTexture("cursors/arrow_idle.png");
    IGUIEnvironment *gui = Device->getGUIEnvironment();
    ScreenCursor = gui->addImage(Icon, position); //put the cursor on the center of the screen
    setCursor(ECI_ARROW_IDLE);
    Device->getCursorControl()->setVisible(false);
    position2d<s32> pos = position2d<s32>(Device->getVideoDriver()->getScreenSize().Width/2,
                                          Device->getVideoDriver()->getScreenSize().Height/2);
    Device->getCursorControl()->setPosition(pos);

    LastMouseMove = Device->getTimer()->getTime(); //now
    EndTime = LastMouseMove + 3500;

    Moveable = true;
    if(Device->getSceneManager()->getActiveCamera()->getType() == ESNT_CAMERA_FPS
        && Device->getSceneManager()->getActiveCamera()->isInputReceiverEnabled())
      {
        Moveable = false;
      }
  }

Cursor::~Cursor()
  {
    //dtor
  }

bool Cursor::OnEvent(const SEvent& event)
  {
    // MOUSE EVENTS
    if (event.EventType == EET_MOUSE_INPUT_EVENT)
      {
        // MOUSE MOVE
        if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
          {
            MouseLoc = Device->getCursorControl()->getPosition();
            LastMouseMove = Device->getTimer()->getTime(); //now
            EndTime = Device->getTimer()->getTime() + 3500;

            // MOVE THE CURSOR

          }
        // MOUSE DOWN
        if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN
            || event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN
            || event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
          {
            ClickLoc = Device->getCursorControl()->getPosition();
            MouseEvent = 1;
            switch (event.MouseInput.Event)
              {
            case EMIE_LMOUSE_PRESSED_DOWN:
              MouseButton = 1;
              break;
            case EMIE_RMOUSE_PRESSED_DOWN:
              MouseButton = 2;
              Device->getSceneManager()->getActiveCamera()->setInputReceiverEnabled(
                              !Device->getSceneManager()->getActiveCamera()->isInputReceiverEnabled());
              break;
            case EMIE_MMOUSE_PRESSED_DOWN:
              MouseButton = 3;
              break;
            default:
              break;
              }
            // Reset mouse event.
            MouseEvent = 0;     //! TODO: maybe to remove :s
          }
      }
    //MOUSE UP
    if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP || event.MouseInput.Event
        == EMIE_RMOUSE_LEFT_UP || event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
      {
        MouseEvent = 2;
        switch (event.MouseInput.Event)
          {
        case EMIE_LMOUSE_LEFT_UP:
          MouseButton = 1;
          break;
        case EMIE_RMOUSE_LEFT_UP:
          MouseButton = 2;
          break;
        case EMIE_MMOUSE_LEFT_UP:
          MouseButton = 3;
          break;
        default:
          break;
          }
        // Reset mouse button
        MouseButton = 0;
        // Reset mouse event
        MouseEvent = 0;
      }
    return false;
  }

void Cursor::draw()
  {
  rect<s32> size = rect<s32> (MouseLoc.X - Hot.X, MouseLoc.Y - Hot.Y,
      MouseLoc.X - Hot.X + Size.Width, MouseLoc.Y - Hot.Y + Size.Height);
  if (Device->getSceneManager()->getActiveCamera()->getID() == -32
      && Device->getSceneManager()->getActiveCamera()->isInputReceiverEnabled())
    {
      position2d<s32> position = position2d<s32> (
          Device->getVideoDriver()->getScreenSize().Width / 2 - Hot.X,
          Device->getVideoDriver()->getScreenSize().Height / 2 - Hot.Y);
      ScreenCursor->setRelativePosition(rect<s32> (position, Size));
    }
  else
    {
      ScreenCursor->setRelativePosition(size); //<-- MOST IMPORTANT
    }
  ScreenCursor->getParent()->bringToFront(ScreenCursor);

    u32 now = Device->getTimer()->getTime();
    SColor color(255, 255, 255, 255);
    f32 d = 1.0f;
    if(now - LastMouseMove >= 2000)
      {
        d = (EndTime - now) / (f32)(EndTime - LastMouseMove);
      }
    if(now > EndTime)
      {
        d = 0;
      }
  SColor newCol = color.getInterpolated(SColor(127,127,127,127), d);
  ScreenCursor->setColor(newCol);
  }

void Cursor::loadCursor(const c8 *filename, position2d<s32> hotPoint)
  {
    Icon = Device->getVideoDriver()->getTexture(filename);
    Hot = hotPoint;
  }

void Cursor::setCursor(ECURSOR_ICON icon)
  {
    if (icon == ECI_HAND_IDLE)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_idle.png");
    if (icon == ECI_HAND_ACTION)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_action.png");
    if (icon == ECI_HAND_FORWARD)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_forward.png");
    if (icon == ECI_HAND_BACK)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_back.png");
    if (icon == ECI_HAND_LEFT)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_left.png");
    if (icon == ECI_HAND_RIGHT)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_right.png");
    if (icon == ECI_HAND_ZOOM)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_zoom.png");
    if (icon == ECI_HAND_ZOOM_IN)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_zoom_in.png");
    if (icon == ECI_HAND_ZOOM_OUT)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_zoom_out.png");
    if (icon == ECI_HAND_PICK)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_pick.png");
    if (icon == ECI_HAND_DROP)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_drop.png");
    if (icon == ECI_HAND_GRAB)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_grab.png");
    if (icon == ECI_HAND_INPUT)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_input.png");
    if (icon == ECI_HAND_NO)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_no.png");
    if (icon == ECI_HAND_HELP)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_help.png");
    if (icon == ECI_HAND_ZIP)
      Icon = Device->getVideoDriver()->getTexture("cursors/hand_zip.png");

    if (icon == ECI_HAND_IDLE || icon == ECI_HAND_ACTION || icon == ECI_HAND_FORWARD
        || icon == ECI_HAND_BACK || icon == ECI_HAND_LEFT || icon == ECI_HAND_RIGHT || icon == ECI_HAND_ZOOM
        || icon == ECI_HAND_ZOOM_IN || icon == ECI_HAND_ZOOM_OUT || icon == ECI_HAND_PICK
        || icon == ECI_HAND_DROP || icon == ECI_HAND_GRAB || icon == ECI_HAND_INPUT || icon == ECI_HAND_NO
        || icon == ECI_HAND_HELP || icon == ECI_HAND_ZIP)
      {
        Hot.X = Icon->getSize().Width / 2;
        Hot.Y = Icon->getSize().Height / 2;
      }


    if (icon == ECI_ARROW_IDLE)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_idle.png");
    if (icon == ECI_ARROW_ACTION)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_action.png");
    if (icon == ECI_ARROW_FORWARD)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_forward.png");
    if (icon == ECI_ARROW_BACK)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_back.png");
    if (icon == ECI_ARROW_LEFT)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_left.png");
    if (icon == ECI_ARROW_RIGHT)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_right.png");
    if (icon == ECI_ARROW_ZOOM)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_zoom.png");
    if (icon == ECI_ARROW_ZOOM_IN)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_zoom_in.png");
    if (icon == ECI_ARROW_ZOOM_OUT)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_zoom_out.png");
    if (icon == ECI_ARROW_PICK)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_pick.png");
    if (icon == ECI_ARROW_DROP)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_drop.png");
    if (icon == ECI_ARROW_GRAB)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_grab.png");
    if (icon == ECI_ARROW_INPUT)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_input.png");
    if (icon == ECI_ARROW_NO)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_no.png");
    if (icon == ECI_ARROW_HELP)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_help.png");
    if (icon == ECI_ARROW_ZIP)
      Icon = Device->getVideoDriver()->getTexture("cursors/arrow_zip.png");

    if (icon == ECI_ARROW_IDLE ||icon ==  ECI_ARROW_ACTION || icon == ECI_ARROW_FORWARD
        || icon == ECI_ARROW_BACK || icon == ECI_ARROW_LEFT || icon == ECI_HAND_RIGHT || icon == ECI_HAND_ZOOM
        || icon == ECI_ARROW_ZOOM_IN || icon == ECI_ARROW_ZOOM_OUT || icon == ECI_ARROW_PICK
        || icon == ECI_ARROW_DROP || icon == ECI_ARROW_GRAB || icon == ECI_ARROW_INPUT || icon == ECI_ARROW_NO
        || icon == ECI_ARROW_HELP || icon == ECI_ARROW_ZIP)
      {
        Hot.X = 5;
        Hot.Y = 5;
      }
    ScreenCursor->setImage(Icon);
    Size = Icon->getSize();
  }

void Cursor::setCursor(s32 index)
{
  const c8* icon[32];
  icon[0] = "cursors/hand_idle.png";
  icon[1] = "cursors/hand_action.png";
  icon[2] = "cursors/hand_forward.png";
  icon[3] = "cursors/hand_back.png";
  icon[4] = "cursors/hand_left.png";
  icon[5] = "cursors/hand_right.png";
  icon[6] = "cursors/hand_zoom.png";
  icon[7] = "cursors/hand_zoom_in.png";
  icon[8] = "cursors/hand_zoom_out.png";
  icon[9] = "cursors/hand_pick.png";
  icon[10] = "cursors/hand_drop.png";
  icon[11] = "cursors/hand_grab.png";
  icon[12] = "cursors/hand_input.png";
  icon[13] = "cursors/hand_no.png";
  icon[14] = "cursors/hand_help.png";
  icon[15] = "cursors/hand_zip.png";
  if(index <= 15)
    {
      Hot.X = Icon->getSize().Width / 2;
      Hot.Y = Icon->getSize().Height / 2;
    }
  icon[16] = "cursors/arrow_idle.png";
  icon[17] = "cursors/arrow_action.png";
  icon[18] = "cursors/arrow_forward.png";
  icon[19] = "cursors/arrow_back.png";
  icon[20] = "cursors/arrow_left.png";
  icon[21] = "cursors/arrow_right.png";
  icon[22] = "cursors/arrow_zoom.png";
  icon[23] = "cursors/arrow_zoom_in.png";
  icon[24] = "cursors/arrow_zoom_out.png";
  icon[25] = "cursors/arrow_pick.png";
  icon[26] = "cursors/arrow_drop.png";
  icon[27] = "cursors/arrow_grab.png";
  icon[28] = "cursors/arrow_input.png";
  icon[29] = "cursors/arrow_no.png";
  icon[30] = "cursors/arrow_help.png";
  icon[31] = "cursors/arrow_zip.png";
  if(index >= 16 && index <= 31)
    {
      Hot.X = 5;
      Hot.Y = 5;
    }
  Icon = Device->getVideoDriver()->getTexture(icon[index]);
  ScreenCursor->setImage(Icon);
  Size = Icon->getSize();
}
